package com.pm2.controller.customer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pm2.dao.customer.CompanyDao;
import com.pm2.entity.customer.Company;
import com.shuohe.util.returnBean.ReturnBean;

@Controller  
@RequestMapping("/pm2/customer")  
public class CompanyController {
	
	
	@Autowired
	private CompanyDao companyDao; 
	
	
	@RequestMapping(value="company/save.do")  
    public @ResponseBody ReturnBean companySave(HttpServletRequest request,HttpServletResponse response){
		Gson reGson = new Gson();
		String data = request.getParameter("data").replaceAll("\"\"", "null");
		
		Company company = reGson.fromJson(data, new TypeToken<Company>(){}.getType());
		
		try
		{
			companyDao.save(company);
			return new ReturnBean(true,"");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,e.getMessage());
		}
	}
	@RequestMapping(value="company/edit.do")  
    public @ResponseBody ReturnBean companyEdit(HttpServletRequest request,HttpServletResponse response){
		Gson reGson = new Gson();
		String data = request.getParameter("data").replaceAll("\"\"", "null");
		
		Company company = reGson.fromJson(data, new TypeToken<Company>(){}.getType());
		
		try
		{
			companyDao.save(company);
			return new ReturnBean(true,"");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,e.getMessage());
		}
	}
	@RequestMapping(value="company/delete.do")  
    public @ResponseBody ReturnBean companyDelete(@RequestParam("id") int id){
	
		try
		{
			companyDao.deleteById(id);
			return new ReturnBean(true,"");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,e.getMessage());
		}
	}
	
	@RequestMapping(value="hello")  
    public String hello(){
		return "hello";
	
		
	}
	
}
